<?php

namespace frontend\modules\orders\models;

use frontend\modules\goods\models\Goods;
use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "order_goods".
 *
 * @property string $rec_id
 * @property string $order_id
 * @property string $goods_id
 * @property string $goods_name
 * @property string $goods_sn
 * @property string $product_id
 * @property integer $goods_number
 * @property string $market_price
 * @property string $goods_price
 * @property string $goods_attr
 * @property integer $send_number
 * @property integer $is_real
 * @property string $extension_code
 * @property string $parent_id
 * @property integer $is_gift
 * @property string $goods_attr_id
 * @property string $order_goods_price
 * @property string $shop_price
 * @property integer $is_promote
 * @property integer $is_delete
 * @property integer $cancel_status
 * @property string $user_id
 * @property string $refere_id
 * @property string $alread_times
 * @property string $share_times
 * @property string $attr_cat_id
 * @property string $goods_image
 * @property string $re_ordersn
 * @property string $re_orderid
 */
class OrderGoods extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_goods';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['rec_id','default','value'=>$this->getUUID()],
            [['rec_id', 'order_id', 'goods_id'], 'required'],
            [['product_id', 'goods_number', 'send_number', 'is_real', 'parent_id', 'is_gift', 'is_promote', 'is_delete', 'cancel_status','share_times','alread_times'], 'integer'],
            [['market_price', 'goods_price', 'order_goods_price', 'shop_price'], 'number'],
            [['goods_attr','goods_image'], 'string'],
            [['rec_id', 'order_id', 'goods_id','user_id','refere_id','attr_cat_id','re_orderid'], 'string', 'max' => 40],
            [['goods_name'], 'string', 'max' => 120],
            [['goods_sn','re_ordersn'], 'string', 'max' => 60],
            [['extension_code'], 'string', 'max' => 30],
            [['goods_attr_id'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'rec_id' => Yii::t('app', 'Rec ID'),
            'order_id' => Yii::t('app', 'Order ID'),
            'goods_id' => Yii::t('app', 'Goods ID'),
            'goods_name' => Yii::t('app', 'Goods Name'),
            'goods_sn' => Yii::t('app', 'Goods Sn'),
            'product_id' => Yii::t('app', 'Product ID'),
            'goods_number' => Yii::t('app', 'Goods Number'),
            'market_price' => Yii::t('app', 'Market Price'),
            'goods_price' => Yii::t('app', 'Goods Price'),
            'goods_attr' => Yii::t('app', 'Goods Attr'),
            'send_number' => Yii::t('app', 'Send Number'),
            'is_real' => Yii::t('app', 'Is Real'),
            'extension_code' => Yii::t('app', 'Extension Code'),
            'parent_id' => Yii::t('app', 'Parent ID'),
            'is_gift' => Yii::t('app', 'Is Gift'),
            'goods_attr_id' => Yii::t('app', 'Goods Attr ID'),
            'order_goods_price' => Yii::t('app', 'Order Goods Price'),
            'shop_price' => Yii::t('app', 'Shop Price'),
            'is_promote' => Yii::t('app', 'Is Promote'),
            'is_delete' => Yii::t('app', 'Is Delete'),
            'cancel_status' => Yii::t('app', 'Cancel Status'),
            'user_id'=>Yii::t('app', 'UserID'),
            'refere_id'=>Yii::t('app', 'RefereID'),
            'alread_times'=>Yii::t('app', 'AlreadTimes'),
            'share_times'=>Yii::t('app', 'ShareTimes'),
            'attr_cat_id'=>Yii::t('app', 'attr_cat_id'),
            'goods_image'=>Yii::t('app', 'goods_image'),
        ];
    }

    /**
     * @inheritdoc
     * @return \backend\modules\orders\models\query\OrderGoodsQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \backend\modules\orders\models\query\OrderGoodsQuery(get_called_class());
    }

    /**
     * 购物车生成货物订单
     *
     * @param $cartARR
     * @return bool
     * @throws Exception
     */
    public function addByCart($cartARR,$order_id)
    {
        static $i = 0;
        foreach($cartARR as $key=>$val) {
            $model = new OrderGoods();
            $i++;
            $model->goods_name = $val['goods_name'];
            $model->user_id = $val['user_id'];
            $model->refere_id = $val['refere_id'];
            $model->re_ordersn = $val['re_ordersn'];
            $model->goods_id = $val['goods_id'];
            $model->market_price = $val['market_price'];
            $model->shop_price = $val['shop_price'];
            $model->goods_number = $val['goods_number'];
            $model->goods_price = $val['cart_price'];
            $model->attr_cat_id = $val['attr_cat_id'];
            $model->goods_sn =  $this->createSn().sprintf("%04d", $i);
            $model->order_id = $order_id;
            $model->goods_image = $val['goods_image'];
            $model->is_gift = $val['is_gift'];
            if(!$model->save()){
                throw new  Exception('货物生成失败！'.json_encode($model->errors));
            }
        }
        return true;
    }

    /**
     * 立即提交
     *
     * @param $post
     * @param $order_id
     * @return bool
     * @throws Exception
     */
    public function addByGoods($post,$order_id)
    {
        $goods = Goods::findOne($post['data']['goodsid']);
        $model = new OrderGoods();
        $model->goods_name = $goods->goods_name;
        $model->user_id = Yii::$app->UserInfo->userid;
        $model->refere_id = Yii::$app->UserInfo->qcode;
        $model->re_ordersn = Yii::$app->session->get('ordersn');
        $model->goods_id = $post['data']['goodsid'];
        $model->goods_number = $post['data']['goods_number'];
        $model->market_price =$goods->market_price;
        $model->shop_price = $goods->shop_price;
        $model->goods_price = $goods->shop_price;
        $model->attr_cat_id = $goods->attr_cat_id;
        $model->goods_sn =  $this->createSn().sprintf("%02d", 1);
        $model->order_id = $order_id;
        $model->is_gift = $goods->is_gift;
        $model->goods_image = $goods->goods_image;
        if(!$model->save()){
            throw new  Exception('货物生成失败！'.json_encode($model->errors));
        }
        return true;
    }

    /**
     * 创建货号
     *
     * @return string
     */
    public function createSn()
    {
        $time = time();
        $sn = date("Ymdhis",$time).rand(1000,999);
        if(static::findOne(['goods_sn'=>$sn])){
            $this->createSn();
        }
        return $sn;
    }
}
